package com.simtop.car.controller;


import com.simtop.car.aop.SystemControllerLog;
import com.simtop.car.common.ServerResponse;
import com.simtop.car.pojo.BrandType;
import com.simtop.car.pojo.User;
import com.simtop.car.service.CarService;
import com.simtop.car.utils.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@CrossOrigin
@RequestMapping(value = "/brand")
public class CarController {

    @Autowired
    public CarService carService;

    private Logger logger= LoggerFactory.getLogger(CarController.class);

    /**
     * 根据汽车类型来获取到汽车的品牌和类型（0是传统车企，1是造车新势力，2是其他）
     * @param request
     * @return
     */
//    @SystemControllerLog
    @ResponseBody
    @RequestMapping(value = "/getCarBrandType",method = RequestMethod.GET)
    public ServerResponse<List<BrandType>> getCarBrandType(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return carService.getCarBrandType();
    }

    /**
     * 新增汽车品牌
     * @param brandType
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/addCarBrandType",method = RequestMethod.POST)
    public  ServerResponse<List<BrandType>> addCarBrandType(@RequestBody BrandType brandType,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return carService.addCarBrandType(brandType,u);
    }
}
